from django.http import HttpResponse
from django.http import HttpRequest
from Response.Resp import Resp
import json
from Model.models import User
from Model.models import AccessRecords


class Admin(object):
    @classmethod
    def queryPersonalInfo(self, request: HttpRequest) -> HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                all_records = AccessRecords.objects.all()
                print(all_records)
                user_list = []
                for item in all_records:
                    one_record = {
                        'update_at': item.update_at,
                        'score': item.final_score,
                        'name': item.uid.name,
                        'pwd':item.uid.pwd,
                        'sex':item.uid.sex,
                        'major':item.uid.major,
                        'email':item.uid.email,
                    }
                    user_list.append(one_record)
                user_list = user_list[::-1]
                resp_json_data = {
                    'userlist': user_list,
                    'rescode': 200,
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)
